/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Administrator
 */
public class KategoriServlet extends HttpServlet{
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
          /*  String JDBC_DRIVER = "com.mysql.jdbc.Driver";
            Class.forName(JDBC_DRIVER);
            String DATABASE_URL = "jdbc:mysql://localhost:3306/test_ikc019?user=root&password=admin";
            
            Connection conn = DriverManager.getConnection(DATABASE_URL);*/
            Connection conn = (Connection)getServletContext().getAttribute("Labb3");
            
            Statement statement;
            statement = conn.createStatement();
            String[] listbox = request.getParameterValues("listbox");
            String album = request.getParameter("album");
            ResultSet resultatMängd = statement.executeQuery("SELECT artist_name, title, recording_id, price, stock_count from music_recordings where category = " + "\"" +listbox[0] + "\"");
            StringBuffer resultat = new StringBuffer();
            
            if(album != null)
            {
                out.println("Du har lagt till en skiva");
            }
            out.println("<html>");
            out.println("<head>");
            out.println("<title>"+ listbox[0] + "</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>"+listbox[0] + "</h1>" );
            out.println("<table>");
            out.println("<tr><th>artist</th><th>titel</th><th>artikelnr</th><th>pris</th><th>lagerstatus</th></tr>");
            while(resultatMängd.next())
            {
                out.println("<tr>");
                for(int i = 1; i <= 6;i++)
                {
                    if(i==2)
                    {
                        out.println("<td><a href=AlbumServlet?album="+resultatMängd.getObject(3)+">"+resultatMängd.getObject(i) + "</a></td>" );
                        
                    }
                    else if(i==6)
                    {
                            out.println("<td><a href=KategoriServlet?listbox=" + listbox[0] + "&album=" + resultatMängd.getObject(3) + ">Lägg till i kundvagn </a> </td>");
                    }
                    else 
                    out.println("<td>" + resultatMängd.getObject(i) + "</td>");
                 
                    
                    
                    //resultat.append(resultatMängd.getObject(i) + "\t");
                }
                out.println("</tr>");
                //resultat.append("\n");
            }
            
            out.println("<h1>Servlet Resultat at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            
            resultatMängd.close();
            statement.close();
            //conn.close();
        } 
       /* catch(ClassNotFoundException e)
        {
            out.println("Fel: " + e);
        }*/
        catch(SQLException e)
        {
            out.println("Fel: " + e);
        }
         finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
